package com.example.swd1.gudong.main.user.runningrank;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.swd1.gudong.R;
import com.example.swd1.gudong.constant.Constant;

import java.util.List;
import java.util.Map;

/**
 * Created by user on 9/12/16.
 */
public class RankAdapter extends BaseAdapter {
    public static final boolean MY_RANK = true;
    public static final boolean CURRENT_RANK = false;
    List<Map<String, Integer>> list;
    Context context;
    private boolean type;


    public RankAdapter(Context context, List<Map<String, Integer>> myRankData, boolean type) {
        list = myRankData;
        this.context = context;
        this.type = type;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            if (type == RankAdapter.MY_RANK) {
                convertView = View.inflate(context, R.layout.item_rank_me, null);
            }
            if (type == RankAdapter.CURRENT_RANK) {
                convertView = View.inflate(context, R.layout.item_rank_current, null);
            }
            viewHolder.rank = (ImageView) convertView.findViewById(R.id.iv_item_rank_rank);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.iv_item_rank_icon);
            viewHolder.name = (TextView) convertView.findViewById(R.id.tv_item_rank_name);
            viewHolder.distance = (TextView) convertView.findViewById(R.id.tv_item_rank_distance);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Map<String, Integer> itemData = list.get(position);
        viewHolder.rank.setImageResource(itemData.get(Constant.RANK));
        viewHolder.icon.setImageResource(itemData.get(Constant.ICON));
        viewHolder.name.setText(itemData.get(Constant.NAME));
        viewHolder.distance.setText(itemData.get(Constant.DISTANCE) / 100.0 + " 公里");
        return convertView;
    }

    private class ViewHolder {
        private ImageView rank;
        private ImageView icon;
        private TextView name;
        private TextView distance;
    }
}
